package com.joint.web.action.com;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.BaseEnum.StateEnum;
import com.fz.us.base.bean.Pager;
import com.joint.base.entity.Company;
import com.joint.base.service.AuthCodeService;
import com.joint.base.service.UsersService;
import com.joint.base.util.DataUtil;
import com.joint.core.entity.ProAttend;
import com.joint.core.entity.ProInfo;
import com.joint.core.service.ProAttendService;
import com.joint.core.service.ProInfoService;
import com.joint.web.action.BaseAdminAction;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by xh on 2015/3/2.
 */
@ParentPackage("com")
public class AjaxProAttendAction extends BaseAdminAction {

    @Resource
    private ProAttendService proAttendService;
    @Resource
    private ProInfoService proInfoService;
    @Resource
    private UsersService usersService;
    @Resource
    private AuthCodeService authCodeService;

    protected ProInfo proInfo;
    protected ProAttend proAttend;

    private String proInfoId;

    private String no;
    private String regularStart;
    private String regularEnd;
    private String unRegularStart;
    private String unRegularEnd;
    private String mark;

    private String regularPeriod;
    private String unRegularPeriod;

    private Date regularStartDate;
    private Date regularEndDate;
    private Date unRegularStartDate;
    private Date unRegularEndDate;

    public String list(){
        pager = new Pager();
        pager.setPageSize(rows);
        pager.setPageNumber(page);
        pager.setOrderBy("createDate");
        pager.setOrderType(BaseEnum.OrderType.desc);

        Company com = usersService.getCompanyByUser();
        //Users user = usersService.getLoginInfo();

        if (StringUtils.isNotEmpty(proInfoId)){
            proInfo = proInfoService.get(proInfoId);
        }else {
            return ajaxHtmlCallback("200", "所属项目信息表丢失！","操作状态");
        }
        //pager = proAttendService.findByPagerAndCompany(pager,null,com,new StateEnum[]{StateEnum.Enable});
        pager = proAttendService.findByPagerAndProInfo(pager,proInfo,new StateEnum[]{StateEnum.Enable});
        List<ProAttend> proAttendList = (List<ProAttend>) pager.getList();

        List<JSONObject> dataRows=new ArrayList<JSONObject>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String,Object> rMap;
        for(ProAttend pa:proAttendList){

            rMap = new HashMap<String, Object>();
            rMap.put("id", pa.getId());
            rMap.put("no", pa.getNo());

            String regularPeriod = DataUtil.DateToString(pa.getRegularStart(), "HH:mm") + " ---- " + DataUtil.DateToString(pa.getRegularEnd(), "HH:mm");
            rMap.put("regularPeriod", regularPeriod);

            String unRegularPeriod = DataUtil.DateToString(pa.getUnRegularStart(), "HH:mm") + " ---- " + DataUtil.DateToString(pa.getUnRegularEnd(), "HH:mm");
            rMap.put("unRegularPeriod", unRegularPeriod);

            rMap.put("mark",pa.getMark());

            dataRows.add(JSONObject.fromObject(rMap));
        }
        long total =pager.getTotalCount();
        if(total%rows==0){
            total = total/rows;
        }else{
            if(total<rows){
                total = 1;
            }else{
                total = total/rows + 1;
            }
        }
        data.put("dataRows",dataRows);
        data.put("page",page);
        data.put("rows",rows);
        data.put("total",total);
        data.put("records",pager.getTotalCount());
        return  ajaxJson(JSONObject.fromObject(data).toString());
    }

    public String view(){
        return "view";
    }

    public String read(){

        return "read";
    }

    public String input(){
        if (StringUtils.isNotEmpty(keyId)){
            proAttend = proAttendService.get(keyId);
            Calendar calendar = Calendar.getInstance();
            Date today = new Date();
            calendar.setTime(today);
            int y = calendar.get(GregorianCalendar.YEAR) - 1900;
            int m = calendar.get(GregorianCalendar.MONTH);
            int d = calendar.get(GregorianCalendar.DATE);
            regularStartDate = proAttend.getRegularStart();
            regularEndDate = proAttend.getRegularEnd();
            unRegularStartDate = proAttend.getUnRegularStart();
            unRegularEndDate = proAttend.getUnRegularEnd();
            //LogUtil.info("y:" + y + " m:" + m + " d:" + d);
            regularStartDate.setYear(y);
            regularEndDate.setYear(y);
            unRegularStartDate.setYear(y);
            unRegularEndDate.setYear(y);

            regularStartDate.setMonth(m);
            regularEndDate.setMonth(m);
            unRegularStartDate.setMonth(m);
            unRegularEndDate.setMonth(m);

            regularStartDate.setDate(d);
            regularEndDate.setDate(d);
            unRegularStartDate.setDate(d);
            unRegularEndDate.setDate(d);
        }
        return  "input";
    }


    public String save(){
        if (StringUtils.isNotEmpty(proInfoId)){
            proInfo = proInfoService.get(proInfoId);
        }else {
            return ajaxHtmlCallback("404", "所属项目信息表丢失！","操作状态");
        }
        //LogUtil.info("keyId:"+keyId);
        if(StringUtils.isNotEmpty(keyId)){
            proAttend = proAttendService.get(keyId);
        }else{
            proAttend = new ProAttend();
        }

        //2个时间区间不能和已经设定的其他考勤设定重复
        Date rs = DataUtil.StringToDate(regularStart, "HH:mm");
        Date re = DataUtil.StringToDate(regularEnd, "HH:mm");
        Date urs = DataUtil.StringToDate(unRegularStart, "HH:mm");
        Date ure = DataUtil.StringToDate(unRegularEnd, "HH:mm");

        Pager pagerPA = new Pager(0);
        pagerPA = proAttendService.findByPagerAndProInfo(pagerPA,proInfo,new StateEnum[]{StateEnum.Enable});
        List<ProAttend> proAttendList = (List<ProAttend>) pagerPA.getList();

        if (proAttendList.size()>0){
            for(ProAttend pa:proAttendList){
                if(pa.getId().equals(keyId)){
                    continue;
                }
                Date ars = pa.getRegularStart();
                Date are = pa.getRegularEnd();
                Date aurs = pa.getUnRegularStart();
                Date aure = pa.getUnRegularEnd();

                if ((DataUtil.MinDiff(rs, ars)<0 && DataUtil.MinDiff(re, ars)>0) || (DataUtil.MinDiff(rs, ars)>=0 && DataUtil.MinDiff(rs, are)<0)){
                    return ajaxHtmlCallback("404", "正常考勤时间区间和已经设定的其他考勤设定重复！","操作状态");
                }
                if ((DataUtil.MinDiff(rs, aurs)<0 && DataUtil.MinDiff(re, aurs)>0) || (DataUtil.MinDiff(rs, aurs)>=0 && DataUtil.MinDiff(rs, aure)<0)){
                    return ajaxHtmlCallback("404", "正常考勤时间区间和已经设定的其他考勤设定重复！","操作状态");
                }
                if ((DataUtil.MinDiff(urs, ars)<0 && DataUtil.MinDiff(ure, ars)>0) || (DataUtil.MinDiff(urs, ars)>=0 && DataUtil.MinDiff(urs, are)<0)){
                    return ajaxHtmlCallback("404", "异常考勤时间区间和已经设定的其他考勤设定重复！","操作状态");
                }
                if ((DataUtil.MinDiff(urs, aurs)<0 && DataUtil.MinDiff(ure, aurs)>0) || (DataUtil.MinDiff(urs, aurs)>=0 && DataUtil.MinDiff(urs, aure)<0)){
                    return ajaxHtmlCallback("404", "异常考勤时间区间和已经设定的其他考勤设定重复！","操作状态");
                }
            }
        }

        proAttend.setRegularStart(rs);
        proAttend.setRegularEnd(re);
        proAttend.setUnRegularStart(urs);
        proAttend.setUnRegularEnd(ure);

        proAttend.setName("");
        no = authCodeService.createAuthCode("proattend");
        proAttend.setNo(no);
        proAttend.setProInfo(proInfo);
        proAttend.setMark(mark);

        proAttend.setCreater(usersService.getLoginInfo());
        proAttend.setCompany(usersService.getCompanyByUser());
        proAttendService.updateAndEnable(proAttend);
        return ajaxHtmlCallback("200", "保存成功！","操作状态");
    }

    public String delete(){
        if(StringUtils.isEmpty(keyId)){
            return ajaxHtmlCallback("404", "请选择客户信息！","操作状态");
        }
        ProAttend proAttend = proAttendService.get(keyId);
        proAttend.setState(StateEnum.Delete);
        proAttendService.update(proAttend);
        return ajaxHtmlCallback("200", "删除成功！","操作状态");
    }

    public ProInfo getProInfo() {
        return proInfo;
    }

    public void setProInfo(ProInfo proInfo) {
        this.proInfo = proInfo;
    }

    public ProAttend getProAttend() {
        return proAttend;
    }

    public void setProAttend(ProAttend proAttend) {
        this.proAttend = proAttend;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getRegularStart() {
        return regularStart;
    }

    public void setRegularStart(String regularStart) {
        this.regularStart = regularStart;
    }

    public String getRegularEnd() {
        return regularEnd;
    }

    public void setRegularEnd(String regularEnd) {
        this.regularEnd = regularEnd;
    }

    public String getUnRegularStart() {
        return unRegularStart;
    }

    public void setUnRegularStart(String unRegularStart) {
        this.unRegularStart = unRegularStart;
    }

    public String getUnRegularEnd() {
        return unRegularEnd;
    }

    public void setUnRegularEnd(String unRegularEnd) {
        this.unRegularEnd = unRegularEnd;
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    public String getRegularPeriod() {
        return regularPeriod;
    }

    public void setRegularPeriod(String regularPeriod) {
        this.regularPeriod = regularPeriod;
    }

    public String getUnRegularPeriod() {
        return unRegularPeriod;
    }

    public void setUnRegularPeriod(String unRegularPeriod) {
        this.unRegularPeriod = unRegularPeriod;
    }

    public String getProInfoId() {
        return proInfoId;
    }

    public void setProInfoId(String proInfoId) {
        this.proInfoId = proInfoId;
    }

    public Date getRegularStartDate() {
        return regularStartDate;
    }

    public void setRegularStartDate(Date regularStartDate) {
        this.regularStartDate = regularStartDate;
    }

    public Date getRegularEndDate() {
        return regularEndDate;
    }

    public void setRegularEndDate(Date regularEndDate) {
        this.regularEndDate = regularEndDate;
    }

    public Date getUnRegularStartDate() {
        return unRegularStartDate;
    }

    public void setUnRegularStartDate(Date unRegularStartDate) {
        this.unRegularStartDate = unRegularStartDate;
    }

    public Date getUnRegularEndDate() {
        return unRegularEndDate;
    }

    public void setUnRegularEndDate(Date unRegularEndDate) {
        this.unRegularEndDate = unRegularEndDate;
    }
}
